package cn.rongdao.jrkabao.ui.deposit.account;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frwidget.dialog.FRDialog;
import com.mifengkong.frwidget.dialog.FRDialogClickListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseLivenessActivity;
import cn.rongdao.jrkabao.bean.deposit.MyAccountBean;
import cn.rongdao.jrkabao.model.deposit.MyAccountModel;
import cn.rongdao.jrkabao.ui.IdentificationVerificate.IdentifySuccessEvent;
import cn.rongdao.jrkabao.ui.cashadvance.addcard.AddDepositCardActivity;
import cn.rongdao.jrkabao.ui.deposit.DepositActivity;
import cn.rongdao.jrkabao.ui.deposit.account.uicomponent.MyAccountUIComponent;
import cn.rongdao.jrkabao.ui.deposit.bill.BillActivity;
import cn.rongdao.jrkabao.ui.deposit.event.DepositAddCardEvent;
import cn.rongdao.jrkabao.ui.deposit.event.DepositSuccessEvent;
import cn.rongdao.jrkabao.ui.deposit.manager.DepositManagerActivity;
import cn.rongdao.jrkabao.ui.identify.ProgressValidateIdCardActivity;
import cn.rongdao.jrkabao.ui.main.MainActivity;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.NumberUtil;

/**
 * Created by jack on 2017/7/28
 *
 * @author jack
 */

public class MyAccountActivity extends BaseLivenessActivity {

    public static void start(Context context) {
        FRStartActivity.start(context, MyAccountActivity.class);
    }

    @BindView(R.id.ama_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.ama_rl_account)
    RelativeLayout rlAccount;
    @BindView(R.id.ama_tv_account_rest)
    TextView tvAccountRest;
    @BindView(R.id.ama_tv_money)
    TextView tvMoney;
    @BindView(R.id.ama_tv_deposit)
    TextView tvDeposit;
    @BindView(R.id.ama_tv_tip)
    TextView tvTip;
    @BindView(R.id.ama_my_account)
    MyAccountUIComponent myAccountUIComponent;

    private boolean isActivityFromSuccess;

    private int isBind;
    private int isIdentity;
    private int isPair;
    private String url;

    @Override
    protected void initView() {
        super.initView();
        initTitleBar();
        initDialog();
        initData();
        initListener();
    }

    private void initData() {
        MyAccountModel.getInstance().execute(new FRBaseObserver<MyAccountBean>(this, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(MyAccountBean data) {
                rlAccount.setVisibility(View.VISIBLE);
                myAccountUIComponent.setContent(data);
                isPair = data.getIs_pair();
                isIdentity = data.getIs_identity();
                isBind = data.getIs_bind();
                tvMoney.setText(FRCollection.appendAsString("¥ ", NumberUtil.getTwoDecimalPlaces(data.getBalance()), ""));
                tvTip.setText(data.getTip());
                tvTip.setVisibility(View.VISIBLE);
                url = data.getUrl();
            }
        });
    }

    private void initListener() {
        tvDeposit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isIdentity == 0) {
                    showDialogIdentification();
                    return;
                } else if (!CommonUtil.isStatusSuccess(isPair)) {
                    startLivenessActiviity(titleBarUIComponent);
                    return;
                } else if (isBind == 2) {
                    DepositManagerActivity.start(thisActivity());
                    return;
                } else if (isBind == 3) {
                    AddDepositCardActivity.start(thisActivity(), true,false);
                    return;
                }
                DepositActivity.start(thisActivity());
            }
        });

        tvAccountRest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(thisActivity(), url);
            }
        });
    }

    private void showDialogIdentification() {
        new FRDialog.CommonBuilder(thisActivity())
                .setContentView(R.layout.dialog_verification)
                .setOnClickListener(R.id.dv_tv_cancel, new FRDialogClickListener() {
                    @Override
                    public boolean onDialogClick(View view) {
                        return true;
                    }
                })
                .setOnClickListener(R.id.dv_tv_go_verify, new FRDialogClickListener() {
                    @Override
                    public boolean onDialogClick(View view) {
                        ProgressValidateIdCardActivity.start(thisActivity(), true, CommonUtil.isStatusSuccess(isPair));
                        return true;
                    }
                })
                .show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void depositAddCardEventMessage(DepositAddCardEvent depositAddCardEvent) {
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void identifySuccessMessage(IdentifySuccessEvent identifySuccessEvent) {
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void depositSuccessEventMessage(DepositSuccessEvent depositSuccessEvent) {
        initData();
        isActivityFromSuccess = true;
    }

    private void initDialog() {
        initLivenessDialog(titleBarUIComponent);

    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("我的账户");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });
        titleBarUIComponent.initRight("账单", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BillActivity.start(thisActivity());
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void finishActivity() {
        if (isActivityFromSuccess) {
            MainActivity.Companion.start(thisActivity());
        }
        finish();
    }

    @Override
    public void onDestroy() {
        myAccountUIComponent.stopPieChartAnim();
        super.onDestroy();
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_my_account;
    }
}